﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class YSJSDao
    {


        public YSJSDao()
        {

            Mapper.CreateMap<Y_YSJS, YSJSModel>();
        }
        public List<Y_YSJS> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_YSJS.Count();

                return edm.Y_YSJS.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public YSJSModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_YSJS.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_YSJS, YSJSModel>(query);
            }

        }


        public int Save(YSJSModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_YSJS.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_YSJS();

                    edm.AddToY_YSJS(query);

                }


                query.ProjectId = obj.ProjectId;

                query.gs = obj.gs;

                query.gsother = obj.gsother;

                query.ysbz = obj.ysbz;

                query.ysbzother = obj.ysbzother;

                query.ysps = obj.ysps;

                query.yspsother = obj.yspsother;

                query.zbxj = obj.zbxj;

                query.jssc = obj.jssc;

                query.jsscother = obj.jsscother;

                query.jsps = obj.jsps;
                query.jsje = obj.jsje;
                query.jspsje = obj.jspsje;
                query.jssjje = obj.jssjje;
                query.jspsother = obj.jspsother;

                query.jssj = obj.jssj;

                query.jssjother = obj.jssjother;
                query.zzsjje = obj.zzsjje;
                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;
                query.createrealname = obj.createrealname;
                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YSJSModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_YSJS.Where(s => s.ProjectId == id&&s.Id==reid);
                 

                return Mapper.Map<Y_YSJS, YSJSModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }


        /// <summary>
        /// 获取最后一次修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YSJSModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_YSJS.Where(s => s.ProjectId == id);


                return Mapper.Map<Y_YSJS, YSJSModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        public List<YSJSModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_YSJS.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_YSJS>, List<YSJSModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }

        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_YSJS.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

